package com.study.h190805bluetooth;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Set;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * Note: 打开蓝牙的状态码
     * 此常量必须大于 0
     */
    public static final int REQUEST_ENABLE_BT = 1;
    public static final String TAG = "MainActivity";
    private Button mPairedButton, mFindButton, mOpenButton;
    private ListView mListView;
    private ArrayList mDeviceData;
    private BluetoothAdapter mBluetoothAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initSubViews();
        configBluetooth();
    }

    /**
     * Note: 配置子控件
     */
    private void initSubViews() {
        mPairedButton = findViewById(R.id.paired_button);
        mPairedButton.setOnClickListener(this);

        mFindButton = findViewById(R.id.find_button);
        mFindButton.setOnClickListener(this);

        mOpenButton = findViewById(R.id.open_button);
        mOpenButton.setOnClickListener(this);

        mListView = findViewById(R.id.list_view);
    }

    /**
     * Note: 配置蓝牙
     */
    private void configBluetooth() {

        // Note: 获取蓝牙适配器  
        final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();

        // Note: 设备不支持蓝牙
        if (mBluetoothAdapter == null){
            showToast("设备不支持蓝牙");
            return;
        }
    }

    /**
     * Note: 查询已配对的设备
     */
    private void searchPairedBluetooth() {

        mDeviceData = new ArrayList();

        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

        //Note：已配对的蓝牙设备不为空
        if (pairedDevices.size() > 0){
            for (BluetoothDevice device: pairedDevices){
                mDeviceData.add(device);
            }

            //Note: 设置 ListView 的适配器
            MyListViewAdapter adapter = new MyListViewAdapter(mDeviceData, this);
            mListView.setAdapter(adapter);
        }
    }

    /**
     * Note: 按钮点击事件
     * @param view
     */
    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.paired_button:
                searchPairedBluetooth();
                break;

            case R.id.find_button:
                //findDevices();
                break;

            default:
                //Note: 打开蓝牙
                openBluetooth();

        }
    }

    private void openBluetooth(){

        // Note: 判断蓝牙是否已打开
        if (mBluetoothAdapter.isEnabled()){
            showToast("蓝牙已经打开");
            //Note: 关闭蓝牙
            //mBluetoothAdapter.disable();

        } else {

            //Note：打开蓝牙 - 方式一
            //boolean isOpen = mBluetoothAdapter.enable();
            //showToast("蓝牙状态: " + isOpen);

            //Note：打开蓝牙 - 方式二
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(intent, REQUEST_ENABLE_BT);
        }

    }

    /**
     * Activity 回调方法
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_ENABLE_BT) {//Note：操作蓝牙

            if (resultCode == RESULT_CANCELED) {
                showToast("蓝牙请求失败");
            } else {
                showToast("蓝牙请求成功");
            }

        } else {//其他操作
            //none
        }
    }

    public void showToast(String msg){
        Toast mToast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
        //Note: 设置 toast 的位置
        mToast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
        mToast.show();
    }
}
